<template>
  <div class="main">
    <div class="search">
      <router-link to="/home/index">
        <div class="return" @click="click"><van-icon name="arrow-left" /></div>
      </router-link>
      <input type="text" placeholder="请输入你要搜索的物品" v-model="content" />
      <div class="iconfont">&#xe602;</div>
      <button @click="search">搜索</button>
    </div>
    <comp-pro-list :searchPro="historyData"></comp-pro-list>
  </div>
</template>

<script>
import { mapState } from "vuex";
import compProList from "@/components/comp-proList.vue";
export default {
  data() {
    return {
      content: "",
      historyData: [],
    };
  },
  computed: {
    ...mapState(["product"]),
  },
  methods: {
    click() {
      console.log(111);
    },
    // 搜索判断
    search() {
      let a = this.content;
      if (a) {
        let reg = /^[0-9]+.?[0-9]*$/;
        if (reg.test(a)) {
          this.historyData = this.product.filter(
            (value) => value.model.indexOf(this.content) !== -1
          );
        } else {
          this.historyData = this.product.filter(
            (value) => value.name.indexOf(this.content) !== -1
          );
        }
      }
      console.log(this.historyData);
    },
  },
  components: {
    compProList,
  },
};
</script>

<style scoped lang="scss">
// 搜索框
.search {
  margin-top: -1px;
  width: 100%;
  height: 70px;
  background: #fafafa;
  position: relative;
  display: flex;
  a {
    flex-basis: 10%;
    height: 80px;
    .return {
      font-size: 24px;
      line-height: 80px;
      text-align: center;
    }
  }
  input {
    display: block;
    flex-basis: 70%;
    height: 40px;
    border-radius: 20px;
    margin-top: 20px;
    border: 1px solid transparent;
    padding: 0 20px 0 5px;
    box-sizing: border-box;
    outline: none;
    font-size: 14px;
  }
  > .iconfont {
    width: 30px;
    height: 30px;
    position: absolute;
    right: 80px;
    top: 25px;
    font-size: 20px;
    text-align: center;
    line-height: 30px;
    color: rgb(155, 155, 155);
  }
  button {
    margin-top: 25px;
    flex-basis: 20%;
    height: 30px;
    background: #f77625;
    border-radius: 20px;
    color: #fff;
    padding: 3px 10px;
    border: none;
    font-size: 14px;
  }
  .content {
    width: 100%;
    background: #fff;
    position: absolute;
    top: 70px;
    left: 0;
    ul {
      width: 100%;
      display: flex;
      flex-direction: column;
      padding: 10px 20px;
      box-sizing: border-box;
      li {
        width: 100%;
        height: 40px;
        line-height: 40px;
        font-size: 16px;
        border-bottom: 1px solid #ccc;
        display: flex;
        > .iconfont {
          margin-right: 10px;
        }
      }
    }
  }
}
</style>